﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using DataCollector.Entity;
using System.Text;

namespace SilverlightApp.data
{
    public class FileWriter
    {
        public DBSelectResult Read(string filePath)
        {
            lock (lockObj)
            {
                if (!File.Exists(filePath))
                {
                    return null;
                }
                while (true)
                {
                    try
                    {
                        string allText = File.ReadAllText(filePath);
                        if (allText != null)
                        {
                            string[] resTextAry = allText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                            if (resTextAry.Length == 1)
                            {
                                string[] ary = resTextAry[0].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                DBSelectResult sr = new DBSelectResult();
                                // sr.Id = int.Parse(ary[0]);
                                sr.ExportTime = ary[0];
                                sr.Temperature = double.Parse(ary[1]);
                                sr.Humidity = double.Parse(ary[2]);
                                sr.Illumination = double.Parse(ary[3]);
                                return sr;
                            }
                        }
                        throw new Exception();
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
        }

        object lockObj = new object();
        public void Write(string file1, string file2, MonitorResult mr)
        {
            lock (lockObj)
            {
                if (!File.Exists(file2))
                {
                    File.Create(file2);
                }
                if (!File.Exists(file1))
                {
                    File.Create(file1);
                }
                string writeText = DateTime.Now.ToString("yyyyMMddHHmmss") + " " + mr.Temperature + " " + mr.Humidity + " " + mr.Illumination;
                try
                {
                    using (StreamWriter sw = File.AppendText(file1))
                    {
                        sw.Write(writeText + Environment.NewLine);
                    }
                }
                catch(IOException)
                {

                }

                while (true)
                {
                    try
                    {
                        FileStream fl1 = new FileStream(file2, FileMode.Open, FileAccess.Write);
                        StreamWriter sw = new StreamWriter(fl1);
                        sw.Write(writeText);
                        sw.Close();
                        fl1.Close();
                        break;
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                }
            }
        }
    }
}